package offer.easy;

public class GetNumbers {

    public static int getNumberForK(int[] array,int k) {
        int left;
        int right;
        int index = array.length/2;
        while (true) {
            if (array[index] > k) {
                index = index/2;
                if (index == 0 && array[index] != k) {
                    return 0;
                }
            } else if (array[index] < k) {
                index = (index+array.length)/2;
                if (index == array.length-1 && array[index] != k) {
                    return 0;
                }
            } else {
                break;
            }
        }
        left = index-1;
        right = index+1;
        while (left >= 0) {
            if (array[left] == k) {
                left--;
            } else {
                break;
            }
        }
        while (right < array.length) {
            if (array[right] == k) {
                right++;
            } else {
                break;
            }
        }
        return (right-left-1);
    }

    public static void main(String[] args) {
        int[] array = {0,3,3,3,3,3,3,3};
        System.out.println(getNumberForK(array, 4));
    }
}
